/*--------管理员系统功能-------*/
#include"admin_system.h"
#include"admin_ui.h"
#include"value.h"
#include"General_func.h"
#include"goods_list_op.h"
#include<stdio.h>
#include<stdlib.h>
#include"../lv_chinese_ime/lv_chinese_ime.h"
#include"../lvgl/src/core/lv_obj.h"
LV_FONT_DECLARE(lv_font_source_han_sans_bold_14);

//上架商品下拉框的回调
void dropdown_event_cb(lv_event_t * e) {
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * target = lv_event_get_target(e);
    char txt[32];
    if(code == LV_EVENT_VALUE_CHANGED) {
        lv_dropdown_get_selected_str(target,txt,32);
        printf("Selected: %s\n", txt);
    }
}

//上架商品界面
void put_goods_cb(void)
{
    // 创建内容容器
    lv_obj_t * cont = lv_win_get_content(input_win);
    lv_obj_set_layout(cont, LV_LAYOUT_FLEX);
    lv_obj_set_flex_flow(cont, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(cont, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_style_pad_all(cont, 5, 0);

    // 商品ID输入
    lv_obj_t * goods_id_cont = lv_obj_create(cont);
    lv_obj_set_size(goods_id_cont, 450,70);
    lv_obj_set_style_border_width(goods_id_cont, 0, 0);
    lv_obj_set_style_pad_all(goods_id_cont, 0, 0);
    lv_obj_remove_style(goods_id_cont, NULL, LV_PART_SCROLLBAR);
    lv_obj_t * id_label = lv_label_create(goods_id_cont);
    lv_label_set_text(id_label, "ID:");
    lv_obj_align(id_label, LV_ALIGN_LEFT_MID, 15, 0);

    goods_id_ta = lv_textarea_create(goods_id_cont);
    lv_obj_center(goods_id_ta);
    lv_obj_set_size(goods_id_ta,250,50);
    lv_obj_remove_style(goods_id_ta, NULL, LV_PART_SCROLLBAR);
    lv_textarea_set_placeholder_text(goods_id_ta, "goods id");
    lv_textarea_set_accepted_chars(goods_id_ta, "0123456789");
    lv_textarea_set_max_length(goods_id_ta, 8);
    lv_textarea_set_one_line(goods_id_ta, true);
    lv_obj_add_event_cb(goods_id_ta,keyboard_event_cb,LV_EVENT_ALL,NULL);

    //商品名称输入
    lv_obj_t * goods_name_cont = lv_obj_create(cont);
    lv_obj_set_size(goods_name_cont, 450, 70);
    lv_obj_set_style_border_width(goods_name_cont, 0, 0);
    lv_obj_set_style_pad_all(goods_name_cont, 0, 0);
    lv_obj_set_style_border_width(goods_name_cont, 0, 0);
    lv_obj_set_style_pad_all(goods_name_cont, 0, 0);
    lv_obj_remove_style(goods_name_cont, NULL, LV_PART_SCROLLBAR);
    lv_obj_t * goods_name_label = lv_label_create(goods_name_cont);
    lv_label_set_text(goods_name_label, "name:");
    lv_obj_align(goods_name_label, LV_ALIGN_LEFT_MID, 10, 0);

    goods_name_ta = lv_textarea_create(goods_name_cont);
    lv_obj_center(goods_name_ta);
    lv_obj_remove_style(goods_name_ta, NULL, LV_PART_SCROLLBAR);
    lv_textarea_set_placeholder_text(goods_name_ta, "goods name");
    lv_textarea_set_max_length(goods_name_ta, 32);
    lv_textarea_set_one_line(goods_name_ta, true);
    lv_obj_add_event_cb(goods_name_ta,keyboard_event_cb,LV_EVENT_ALL,NULL);

    //商品价格输入
    lv_obj_t * goods_price_cont = lv_obj_create(cont);
    lv_obj_set_size(goods_price_cont, 450, 70);
    lv_obj_set_style_border_width(goods_price_cont, 0, 0);
    lv_obj_set_style_pad_all(goods_price_cont, 0, 0);
    lv_obj_align_to(goods_price_cont,goods_name_cont,LV_ALIGN_OUT_BOTTOM_MID,0,3);
    lv_obj_remove_style(goods_price_cont, NULL, LV_PART_SCROLLBAR);
    lv_obj_t * goods_price_label = lv_label_create(goods_price_cont);
    lv_label_set_text(goods_price_label, "price:");
    lv_obj_align(goods_price_label, LV_ALIGN_LEFT_MID, 5, 0);

    goods_price_ta = lv_textarea_create(goods_price_cont);
    lv_obj_center(goods_price_ta);
    lv_obj_remove_style(goods_price_ta, NULL, LV_PART_SCROLLBAR);
    lv_textarea_set_placeholder_text(goods_price_ta, "goods price");
    lv_textarea_set_accepted_chars(goods_price_ta, "0123456789");
    lv_textarea_set_max_length(goods_price_ta, 10);
    lv_textarea_set_one_line(goods_price_ta, true);
    lv_obj_add_event_cb(goods_price_ta,keyboard_event_cb,LV_EVENT_ALL,NULL);

    //商品数量输入
    lv_obj_t * goods_quantity_cont = lv_obj_create(cont);
    lv_obj_set_size(goods_quantity_cont, 450, 70);
    lv_obj_set_style_border_width(goods_quantity_cont, 0, 0);
    lv_obj_set_style_pad_all(goods_quantity_cont, 0, 0);
    lv_obj_align_to(goods_quantity_cont,goods_price_cont,LV_ALIGN_OUT_BOTTOM_MID,0,3);
    lv_obj_remove_style(goods_quantity_cont, NULL, LV_PART_SCROLLBAR);
    lv_obj_t * goods_quantity_label = lv_label_create(goods_quantity_cont);
    lv_label_set_text(goods_quantity_label, "quantity:");
    lv_obj_align(goods_quantity_label, LV_ALIGN_LEFT_MID, 5, 0);

    goods_quantity_ta = lv_textarea_create(goods_quantity_cont);
    lv_obj_center(goods_quantity_ta);
    lv_obj_remove_style(goods_quantity_ta, NULL, LV_PART_SCROLLBAR);
    lv_textarea_set_placeholder_text(goods_quantity_ta, "goods price");
    lv_textarea_set_accepted_chars(goods_quantity_ta, "0123456789");
    lv_textarea_set_max_length(goods_quantity_ta, 10);
    lv_textarea_set_one_line(goods_quantity_ta, true);
    lv_obj_add_event_cb(goods_quantity_ta,keyboard_event_cb,LV_EVENT_ALL,NULL);

    //商品种类输入
    lv_obj_t * goods_type_cont = lv_obj_create(cont);
    lv_obj_set_size(goods_type_cont, 450, 70);
    lv_obj_set_style_border_width(goods_type_cont, 0, 0);
    lv_obj_set_style_pad_all(goods_type_cont, 0, 0);
    lv_obj_align_to(goods_type_cont,goods_quantity_cont,LV_ALIGN_OUT_BOTTOM_MID,0,3);
    lv_obj_remove_style(goods_type_cont, NULL, LV_PART_SCROLLBAR);
    lv_obj_t * goods_type_label = lv_label_create(goods_type_cont);
    lv_label_set_text(goods_type_label, "type:");
    lv_obj_align(goods_type_label, LV_ALIGN_LEFT_MID, 5, 0);

    goods_type_ta = lv_dropdown_create(goods_type_cont);
    //lv_obj_set_style_text_font(goods_type_ta, &lv_font_source_han_sans_bold_14, 0);
    lv_dropdown_set_options(goods_type_ta, "snacks");//零食
    lv_dropdown_add_option(goods_type_ta, "Fruit", 1);//水果
    lv_dropdown_add_option(goods_type_ta, "vegetables", 2);//蔬菜
    lv_dropdown_add_option(goods_type_ta, "Household", 3);//家具
    lv_dropdown_add_option(goods_type_ta, "Beverage", 4);//饮料
    lv_dropdown_add_option(goods_type_ta, "clothing", LV_DROPDOWN_POS_LAST);//服装
    lv_obj_set_size(goods_type_ta,290,40);
    lv_obj_center(goods_type_ta);
    lv_obj_invalidate(goods_type_ta);
    lv_dropdown_set_selected(goods_type_ta, 0);
    // lv_obj_add_event_cb(goods_type_ta, dropdown_event_cb, LV_EVENT_VALUE_CHANGED, NULL);

    
    // 完成按钮
    lv_obj_t * put_btn = lv_btn_create(cont);
    lv_obj_set_size(put_btn,100,30);

    lv_obj_t * put_btn_label = lv_label_create(put_btn);
    lv_label_set_text(put_btn_label, "Put");
    lv_obj_center(put_btn_label);
    lv_obj_add_event_cb(put_btn, finish_btn_event, LV_EVENT_CLICKED, NULL);
}

//下架商品界面
void remove_goods_cb(void)
{
    // 创建内容容器
    lv_obj_t * cont = lv_win_get_content(input_win);
    lv_obj_set_style_border_width(cont, 0, 0);
    lv_obj_set_style_pad_all(cont, 0, 0);
    //2.创建搜索框
    lv_obj_t * find_goods_cont = lv_obj_create(cont);
    lv_obj_set_size(find_goods_cont, 800,80);
    lv_obj_set_style_border_width(find_goods_cont, 0, 0);
    lv_obj_set_style_pad_all(find_goods_cont, 0, 0);
    lv_obj_remove_style(find_goods_cont, NULL, LV_PART_SCROLLBAR);
    //创建一个表格容器
    lv_obj_t * goods_table_cont = lv_obj_create(cont);
    lv_obj_set_size(goods_table_cont, 800,350);
    lv_obj_align_to(goods_table_cont,find_goods_cont,LV_ALIGN_OUT_BOTTOM_MID,0,0);
    lv_obj_set_style_border_width(goods_table_cont, 0, 0);
    lv_obj_set_style_pad_all(goods_table_cont, 0, 0);
    lv_obj_remove_style(goods_table_cont, NULL, LV_PART_SCROLLBAR);

    //  2.1   id
    lv_obj_t * find_id_label = lv_label_create(find_goods_cont);
    lv_label_set_text(find_id_label, "ID:");
    lv_obj_align(find_id_label, LV_ALIGN_LEFT_MID, 80, 0);

    lv_obj_t *find_goods_id = lv_textarea_create(find_goods_cont);
    lv_obj_align(find_goods_id,LV_ALIGN_LEFT_MID,105,0);
    lv_obj_set_size(find_goods_id,150,50);
    lv_obj_remove_style(find_goods_id, NULL, LV_PART_SCROLLBAR);
    lv_textarea_set_placeholder_text(find_goods_id, "goods id");
    lv_textarea_set_accepted_chars(find_goods_id, "0123456789");
    lv_textarea_set_max_length(find_goods_id, 8);
    lv_textarea_set_one_line(find_goods_id, true);
    lv_obj_add_event_cb(find_goods_id,keyboard_event_cb,LV_EVENT_ALL,NULL);

    //  2.2   name
    lv_obj_t * find_name_label = lv_label_create(find_goods_cont);
    lv_label_set_text(find_name_label, "name:");
    lv_obj_align_to(find_name_label,find_goods_id, LV_ALIGN_OUT_RIGHT_MID, 10, 0);

    lv_obj_t *find_goods_name = lv_textarea_create(find_goods_cont);
    lv_obj_align(find_goods_name,LV_ALIGN_LEFT_MID,315,0);
    lv_obj_set_size(find_goods_name,150,50);
    lv_obj_remove_style(find_goods_name, NULL, LV_PART_SCROLLBAR);
    lv_textarea_set_placeholder_text(find_goods_name, "goods name");
    lv_textarea_set_max_length(find_goods_name, 8);
    lv_textarea_set_one_line(find_goods_name, true);
    lv_obj_add_event_cb(find_goods_name,keyboard_event_cb,LV_EVENT_ALL,NULL);
    
    //  2.3   type
    lv_obj_t * find_type_label = lv_label_create(find_goods_cont);
    lv_label_set_text(find_type_label, "type:");
    lv_obj_align_to(find_type_label,find_goods_name, LV_ALIGN_OUT_RIGHT_MID, 5, 0);

    lv_obj_t *find_goods_type = lv_dropdown_create(find_goods_cont);
    lv_dropdown_set_options(find_goods_type, "snacks");//零食
    lv_dropdown_add_option(find_goods_type, "Fruit", 1);//水果
    lv_dropdown_add_option(find_goods_type, "vegetables", 2);//蔬菜
    lv_dropdown_add_option(find_goods_type, "Household", 3);//家具
    lv_dropdown_add_option(find_goods_type, "Beverage", 4);//饮料
    lv_dropdown_add_option(find_goods_type, "clothing", 5);//服装
    lv_dropdown_add_option(find_goods_type, "", LV_DROPDOWN_POS_LAST);//空选项
    lv_obj_set_size(find_goods_type,150,45);
    lv_obj_align_to(find_goods_type,find_type_label,LV_ALIGN_OUT_RIGHT_MID,3,0);
    lv_obj_invalidate(find_goods_type);
    lv_dropdown_set_selected(find_goods_type, 6);



    FindData *find_data =malloc(sizeof(FindData));
    if (find_data == NULL) {
        printf("Memory allocation failed\n");
        return;
    }
    find_data->find_goods_id_ta = find_goods_id;
    find_data->find_goods_name_ta= find_goods_name;
    find_data->find_goods_type_dp= find_goods_type;
    find_data->goods_table_cont = goods_table_cont;


    //  2.4  查找按钮
    lv_obj_t *find_btn = lv_btn_create(find_goods_cont);
    lv_obj_set_size(find_btn,80,45);
    lv_obj_align_to(find_btn,find_goods_type, LV_ALIGN_OUT_RIGHT_MID, 10, 0);
    lv_obj_add_event_cb(find_btn,find_goods_event_cb,LV_EVENT_CLICKED,(void *)find_data);
    lv_obj_t * find_btn_label = lv_label_create(find_btn);
    lv_label_set_text(find_btn_label, "find");
    lv_obj_center(find_btn_label);
    
}

/*显示商品*/
void show_goods_cb(void)
{
    // 创建内容容器
    lv_obj_t * show_goods_cont = lv_win_get_content(input_win);


    //创建表格对象
    lv_obj_t *table =lv_table_create(show_goods_cont);
    lv_obj_set_size(table,625,440);
    lv_obj_center(table);
    lv_obj_set_style_border_width(table, 0, 0);
    lv_obj_set_style_pad_all(table, 0, 0);
    // 设置表格样式
    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_border_width(&style, 1);                   // 边框宽度
    lv_style_set_border_color(&style, lv_palette_main(LV_PALETTE_BLUE)); // 边框颜色
    lv_style_set_bg_color(&style, lv_color_hex(0xf0f0f0));  // 背景色
    lv_style_set_pad_all(&style, 5);                        // 内边距
    //应用样式
    lv_obj_add_style(table, &style, 0);
    //设置表格的行列数
    lv_table_set_col_cnt(table,5);
    lv_table_set_row_cnt(table,goods_count+1);
    //设置列宽
    lv_table_set_col_width(table, 0, 125);  // 第 0 列宽度 100px
    lv_table_set_col_width(table, 1, 125);  // 第 1 列宽度 100px
    lv_table_set_col_width(table, 2, 125);  // 第 2 列宽度 100px
    lv_table_set_col_width(table, 3, 125);  // 第 2 列宽度 100px
    lv_table_set_col_width(table, 4, 125);  // 第 2 列宽度 100px
    //表头设置
    lv_table_set_cell_value(table, 0, 0, "Id");    
    lv_table_set_cell_value(table, 0, 1, "Name");    
    lv_table_set_cell_value(table, 0, 2, "price");
    lv_table_set_cell_value(table, 0, 3, "quantity");
    lv_table_set_cell_value(table, 0, 4, "type");
   
    char buf_id[20];
    char buf_price[10];
    char buf_quantity[20];
    
    Goodslist *tmp = goods_list->next;
    int i=1;
    for(;tmp!=NULL;tmp=tmp->next,i++)
    {
        //转换成字符
        snprintf(buf_id,sizeof(buf_id),"%d",tmp->id);
        snprintf(buf_price,sizeof(buf_price),"%.1f",tmp->price);
        snprintf(buf_quantity,sizeof(buf_quantity),"%d",tmp->quantity); 

        lv_table_set_cell_value(table, i, 0, buf_id);    
        lv_table_set_cell_value(table, i, 1, tmp->name);    
        lv_table_set_cell_value(table, i, 2, buf_price);
        lv_table_set_cell_value(table, i, 3, buf_quantity);
        lv_table_set_cell_value(table, i, 4, tmp->type);
    }

}

//显示全部商品
void show_all_goods_cb(lv_obj_t *cont)
{
    //创建表格对象
    lv_obj_t *table =lv_table_create(cont);
    lv_obj_set_size(table,625,350);
    lv_obj_align(table,LV_ALIGN_TOP_MID,5,0);
    lv_obj_set_style_border_width(table, 0, 0);
    lv_obj_set_style_pad_all(table, 0, 0);
    // 设置表格样式
    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_border_width(&style, 1);                   // 边框宽度
    lv_style_set_border_color(&style, lv_palette_main(LV_PALETTE_BLUE)); // 边框颜色
    lv_style_set_bg_color(&style, lv_color_hex(0xf0f0f0));  // 背景色
    lv_style_set_pad_all(&style, 5);                        // 内边距
    //应用样式
    lv_obj_add_style(table, &style, 0);
    //设置表格的行列数
    lv_table_set_col_cnt(table,5);
    lv_table_set_row_cnt(table,goods_count+1);
    //设置列宽
    lv_table_set_col_width(table, 0, 125);  // 第 0 列宽度 100px
    lv_table_set_col_width(table, 1, 125);  // 第 1 列宽度 100px
    lv_table_set_col_width(table, 2, 125);  // 第 2 列宽度 100px
    lv_table_set_col_width(table, 3, 125);  // 第 2 列宽度 100px
    lv_table_set_col_width(table, 4, 125);  // 第 2 列宽度 100px
    //表头设置
    lv_table_set_cell_value(table, 0, 0, "Id");    
    lv_table_set_cell_value(table, 0, 1, "Name");    
    lv_table_set_cell_value(table, 0, 2, "price");
    lv_table_set_cell_value(table, 0, 3, "quantity");
    lv_table_set_cell_value(table, 0, 4, "type");
   
    char buf_id[20];
    char buf_price[10];
    char buf_quantity[20];
    
    Goodslist *tmp = goods_list->next;
    int i=1;
    for(;tmp!=NULL;tmp=tmp->next,i++)
    {
        //转换成字符
        snprintf(buf_id,sizeof(buf_id),"%d",tmp->id);
        snprintf(buf_price,sizeof(buf_price),"%.1f",tmp->price);
        snprintf(buf_quantity,sizeof(buf_quantity),"%d",tmp->quantity); 

        lv_table_set_cell_value(table, i, 0, buf_id);    
        lv_table_set_cell_value(table, i, 1, tmp->name);    
        lv_table_set_cell_value(table, i, 2, buf_price);
        lv_table_set_cell_value(table, i, 3, buf_quantity);
        lv_table_set_cell_value(table, i, 4, tmp->type);
    }
}

//下架商品确认框
void msgbox_event_handler(lv_event_t *e) {
    lv_obj_t *target = lv_event_get_current_target(e);
    char *id_str = (char*)lv_event_get_user_data(e); 
    int id = atoi(id_str);
    // 获取用户点击的按钮文本
    char btn_txt[32];
    strcpy(btn_txt,lv_msgbox_get_active_btn_text(target));
    if (strcmp(btn_txt, "Yes") == 0) {
        Goodslist *delete_node =  find_node(goods_list,id);
        remove_node(goods_list,delete_node);
        file_save_data(goods_list,"goodsdata.txt");
        char buf[256];
        snprintf(buf,sizeof(buf),"%s have been successfully delete.",delete_node->name);
        log_add(LOG_DEL_ITEM,cur_user.username,cur_user.identity,buf);
        remove_goods_cb();
    }

    // 关闭消息框
    lv_msgbox_close(target);
}

//下架商品的回调
void delete_goods_event_cb(lv_event_t *e)
{
    lv_obj_t *table = lv_event_get_target(e);
    uint16_t  row, col;

    // 获取点击的行和列
    lv_table_get_selected_cell(table, &row, &col);
    const char * cell_text = lv_table_get_cell_value(table, row, 0);
    printf("%s\n",cell_text);
    // printf("%hu\n",col);
    // printf("%hu\n",row);
    // 如果点击的是操作列（第三列）
    if (col == 5 && row !=0) {
        // 创建消息框，标题为"Confirm"，内容为"Delete this item?"
        static const char *btns[]  = {"Yes","No",""};
        lv_obj_t * mbox = lv_msgbox_create(NULL, "Confirm", "Delete this item?", btns, true);
        lv_obj_center(mbox);
        // 绑定消息框事件，传递要删除的行号（通过 user_data）
        lv_obj_add_event_cb(mbox, msgbox_event_handler, LV_EVENT_VALUE_CHANGED, cell_text);
        // 居中显示消息框
        lv_obj_center(mbox);
    }
}

//下架商品中显示查找的商品
void show_find_goods_cb(int id,char *name,char *type,int flag,lv_obj_t *cont)
{
    //创建表格对象
    lv_obj_t *find_goods_table =lv_table_create(cont);
    lv_obj_set_size(find_goods_table,720,350);
    lv_obj_set_style_border_width(find_goods_table, 0, 0);
    lv_obj_set_style_pad_all(find_goods_table, 0, 0);
    lv_obj_align(find_goods_table,LV_ALIGN_TOP_MID,0,0);
    // 设置表格样式
    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_border_width(&style, 1);                   // 边框宽度
    lv_style_set_border_color(&style, lv_palette_main(LV_PALETTE_BLUE)); // 边框颜色
    lv_style_set_bg_color(&style, lv_color_hex(0xf0f0f0));  // 背景色
    lv_style_set_pad_all(&style, 5);                        // 内边距
    //应用样式
    lv_obj_add_style(find_goods_table, &style, 0);
    //设置表格的行列数
    lv_table_set_col_cnt(find_goods_table,6);
    int row = goods_count+1;
    if(id != 0)
    {
        row =2;
    }
    lv_table_set_row_cnt(find_goods_table,row);
    //设置列宽
    lv_table_set_col_width(find_goods_table, 0, 120);  // 第 0 列宽度 100px
    lv_table_set_col_width(find_goods_table, 1, 120);  // 第 1 列宽度 100px
    lv_table_set_col_width(find_goods_table, 2, 120);  // 第 2 列宽度 100px
    lv_table_set_col_width(find_goods_table, 3, 120);  // 第 2 列宽度 100px
    lv_table_set_col_width(find_goods_table, 4, 120);  // 第 2 列宽度 100px
    lv_table_set_col_width(find_goods_table, 5, 120);  // 第 2 列宽度 100px
    //表头设置
    lv_table_set_cell_value(find_goods_table, 0, 0, "Id");    
    lv_table_set_cell_value(find_goods_table, 0, 1, "Name");    
    lv_table_set_cell_value(find_goods_table, 0, 2, "price");
    lv_table_set_cell_value(find_goods_table, 0, 3, "quantity");
    lv_table_set_cell_value(find_goods_table, 0, 4, "type");
    lv_table_set_cell_value(find_goods_table, 0, 5, "");


    char buf_id[20];
    char buf_price[10];
    char buf_quantity[20];
    int i=1;
    // lv_obj_t * delete_btn;
    // lv_obj_t * delete_label;
    if(flag == 0)//根据id
    {
        Goodslist *find_goods = find_node(goods_list,id);
        snprintf(buf_id,sizeof(buf_id),"%d",find_goods->id);
        snprintf(buf_price,sizeof(buf_price),"%.1f",find_goods->price);
        snprintf(buf_quantity,sizeof(buf_quantity),"%d",find_goods->quantity); 

        lv_table_set_cell_value(find_goods_table, 1, 0, buf_id);    
        lv_table_set_cell_value(find_goods_table, 1, 1, find_goods->name);    
        lv_table_set_cell_value(find_goods_table, 1, 2, buf_price);
        lv_table_set_cell_value(find_goods_table, 1, 3, buf_quantity);
        lv_table_set_cell_value(find_goods_table, 1, 4, find_goods->type);
        lv_table_set_cell_value(find_goods_table, 1, 5, LV_SYMBOL_CLOSE);

    }
    else
    {
        Goodslist *find_goods=goods_list->next;
        for(;find_goods!=NULL;find_goods=find_goods->next)
        {
            snprintf(buf_id,sizeof(buf_id),"%d",find_goods->id);
            snprintf(buf_price,sizeof(buf_price),"%.1f",find_goods->price);
            snprintf(buf_quantity,sizeof(buf_quantity),"%d",find_goods->quantity); 
            if(flag == 1)
            {
                if(strcmp(find_goods->name,name) != 0)
                {
                    continue;
                }
            }
            else if(flag == 2)
            {
                if((strcmp(find_goods->name,name) != 0 )|| (strcmp(find_goods->type,type) != 0 ))
                {
                    continue;
                }
            }
            else if(flag == 3)
            {
                if((strcmp(find_goods->type,type) != 0 ))
                {
                    continue;
                }
            }
            lv_table_set_cell_value(find_goods_table, i, 0, buf_id);    
            lv_table_set_cell_value(find_goods_table, i, 1, find_goods->name);    
            lv_table_set_cell_value(find_goods_table, i, 2, buf_price);
            lv_table_set_cell_value(find_goods_table, i, 3, buf_quantity);
            lv_table_set_cell_value(find_goods_table, i, 4, find_goods->type);
            lv_table_set_cell_value(find_goods_table, i, 5, LV_SYMBOL_CLOSE);
            i++;

            // delete_btn = lv_btn_create(cont);
            // lv_obj_set_size(delete_btn, 50, 40);  // 设置按钮大小
            // lv_obj_set_pos(delete_btn,710,(i-1)*60);// 相对于每行的中间偏右放置按钮
            // lv_obj_add_event_cb(delete_btn,delete_goods_event_cb,LV_EVENT_CLICKED,find_goods);
            // delete_label = lv_label_create(delete_btn);
            // lv_label_set_text(delete_label,LV_SYMBOL_CLOSE);
            // lv_obj_center(delete_label);
        }
    }
    lv_obj_add_event_cb(find_goods_table, delete_goods_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
}

/*确定查找商品的条件:id、name、type*/
void find_goods_event_cb(lv_event_t *e)
{

    lv_obj_t *target = lv_event_get_target(e);
    lv_obj_t *label = lv_obj_get_child(target,0);
    char *txt = lv_label_get_text(label);
    int find_flag = 0;//默认是下架商品
    if(strcmp(txt,"Search")==0)
    {
        find_flag = 1;
    }
    FindData *find_data = (FindData*)lv_event_get_user_data(e);
    if(find_data == NULL)
    {
        printf("Error:selete_data is NULL\n");
        return;
    }

    char *find_goods_id = lv_textarea_get_text(find_data->find_goods_id_ta);
    char *find_goods_name;
    char find_goods_type[32];

    if(find_flag == 0)
    {

        find_goods_name = lv_textarea_get_text(find_data->find_goods_name_ta);
        lv_dropdown_get_selected_str(find_data->find_goods_type_dp,find_goods_type,32);
    }
   
    int id_len = strlen(find_goods_id);

    int name_len = 0;
    int type_len = 0;
    if(find_flag == 0)
    {
        name_len = strlen(find_goods_name);
        type_len = strlen(find_goods_type);
    }

    int id = 0;
    if(id_len != 0)
    {
        id = atoi(find_goods_id);
    }

    if(id_len == 0 && name_len == 0 && type_len == 0)
    {
        if(find_flag == 1)
        {
            show_all_goods_cb(find_data->goods_table_cont);
            return;
        }
        else{
            lv_obj_t * mbox = lv_msgbox_create(NULL, "Reminder", "At least search for one item.", NULL, true);
            lv_obj_center(mbox);
            return;
        }
    }
    int flag=0;
    if(id_len != 0)
    {
        Goodslist *find_goods = find_node(goods_list,id);
        if(find_goods == NULL)
        {
            lv_obj_t * mbox = lv_msgbox_create(NULL, "Reminder", "The item is not found.", NULL, true);
            lv_obj_center(mbox);
            return;
        }
        if(name_len != 0)
        {
            if(strcmp(find_goods_name,find_goods->name) != 0)
            {
                lv_obj_t * mbox = lv_msgbox_create(NULL, "Reminder", "The item is not found.", NULL, true);
                lv_obj_center(mbox);
                return;
            }   
        }
        if(type_len != 0)
        {
            if(strcmp(find_goods_type,find_goods->type) != 0)
            {
                lv_obj_t * mbox = lv_msgbox_create(NULL, "Reminder", "The item is not found.", NULL, true);
                lv_obj_center(mbox);
                return;
            }   
        }
        if(find_flag == 1)
        {
            Inventory_show_search_goods_cb(id,find_data->goods_table_cont);
        }else{
            //绘制表格
            show_find_goods_cb(id,find_goods_name,find_goods_type,flag,find_data->goods_table_cont);
        }

    }else
    {
        if(name_len != 0)
        {
            flag++;
            Goodslist *find_goods = find_node_by_name(goods_list,find_goods_name);
            if(find_goods == NULL)
            {
                lv_obj_t * mbox = lv_msgbox_create(NULL, "Reminder", "The item is not found.", NULL, true);
                lv_obj_center(mbox);
                return;
            }

            if(type_len != 0)
            {
                if(strcmp(find_goods_type,find_goods->type) != 0)
                {
                    lv_obj_t * mbox = lv_msgbox_create(NULL, "Reminder", "The item is not found.", NULL, true);
                    lv_obj_center(mbox);
                    return;
                }   
                flag++;
            }
            show_find_goods_cb(id,find_goods_name,find_goods_type,flag,find_data->goods_table_cont);
        }
        else{
            Goodslist *find_goods = find_node_by_type(goods_list,find_goods_type);
            if(find_goods == NULL)
            {
                lv_obj_t * mbox = lv_msgbox_create(NULL, "Reminder", "The item is not found.", NULL, true);
                lv_obj_center(mbox);
                return;
            }
            show_find_goods_cb(id,find_goods_name,find_goods_type,3,find_data->goods_table_cont);
        }
    }

}

//库存管理根据id搜索的商品展示
void Inventory_show_search_goods_cb(int id,lv_obj_t *cont)
{
    //创建表格对象
    lv_obj_t *find_goods_table =lv_table_create(cont);
    lv_obj_set_size(find_goods_table,600,350);
    lv_obj_set_style_border_width(find_goods_table, 0, 0);
    lv_obj_set_style_pad_all(find_goods_table, 0, 0);
    lv_obj_align(find_goods_table,LV_ALIGN_TOP_MID,0,0);

    // 设置表格样式
    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_border_width(&style, 1);                   // 边框宽度
    lv_style_set_border_color(&style, lv_palette_main(LV_PALETTE_BLUE)); // 边框颜色
    lv_style_set_bg_color(&style, lv_color_hex(0xf0f0f0));  // 背景色
    lv_style_set_pad_all(&style, 5);                        // 内边距
    //应用样式
    lv_obj_add_style(find_goods_table, &style, 0);
    //设置表格的行列数
    lv_table_set_col_cnt(find_goods_table,5);
    lv_table_set_row_cnt(find_goods_table,2);

    //设置列宽
    lv_table_set_col_width(find_goods_table, 0, 120);  // 第 0 列宽度 100px
    lv_table_set_col_width(find_goods_table, 1, 120);  // 第 1 列宽度 100px
    lv_table_set_col_width(find_goods_table, 2, 120);  // 第 2 列宽度 100px
    lv_table_set_col_width(find_goods_table, 3, 120);  // 第 2 列宽度 100px
    lv_table_set_col_width(find_goods_table, 4, 120);  // 第 2 列宽度 100px

    //表头设置
    lv_table_set_cell_value(find_goods_table, 0, 0, "Id");    
    lv_table_set_cell_value(find_goods_table, 0, 1, "Name");    
    lv_table_set_cell_value(find_goods_table, 0, 2, "price");
    lv_table_set_cell_value(find_goods_table, 0, 3, "quantity");
    lv_table_set_cell_value(find_goods_table, 0, 4, "type");

    char buf_id[20];
    char buf_price[10];
    char buf_quantity[20];
    Goodslist *find_goods = find_node(goods_list,id);
    snprintf(buf_id,sizeof(buf_id),"%d",find_goods->id);
    snprintf(buf_price,sizeof(buf_price),"%.1f",find_goods->price);
    snprintf(buf_quantity,sizeof(buf_quantity),"%d",find_goods->quantity); 

    lv_table_set_cell_value(find_goods_table, 1, 0, buf_id);    
    lv_table_set_cell_value(find_goods_table, 1, 1, find_goods->name);    
    lv_table_set_cell_value(find_goods_table, 1, 2, buf_price);
    lv_table_set_cell_value(find_goods_table, 1, 3, buf_quantity);
    lv_table_set_cell_value(find_goods_table, 1, 4, find_goods->type);
}

// 库存管理改变商品数量(增加、减少)
void change_quantity_event_cb(lv_event_t *e)
{
    lv_obj_t *target = lv_event_get_target(e);
    lv_obj_t *label = lv_obj_get_child(target,0);
    char *txt = lv_label_get_text(label);
    int select_flag = 0;//默认减
    if(strcmp(txt,LV_SYMBOL_PLUS) == 0)
    {
        select_flag = 1;
    }
    FindData *data = (FindData*)lv_event_get_user_data(e);
    if(data == NULL)
    {
        printf("Error:selete_data is NULL\n");
        return;
    }
    char *data_id = lv_textarea_get_text(data->find_goods_id_ta);
    char *add_number = lv_textarea_get_text(data->find_goods_name_ta);
    char *reduce_number =lv_textarea_get_text(data->find_goods_type_dp);

    int id_len = strlen(data_id);
    int add_len = strlen(add_number);
    int reduce_len = strlen(reduce_number);

    if(id_len == 0)
    {
        lv_obj_t * mbox = lv_msgbox_create(NULL, "Reminder", "Please enter the product ID.", NULL, true);
        lv_obj_center(mbox);
        return;
    }

    if(add_len == 0 && reduce_len == 0)
    {
        return;
    }
    int id =atoi(data_id);
    int final_quantity;
    Goodslist *find_goods = find_node(goods_list,id);
    char log_buf[256];
    if( (add_len != 0) && (select_flag == 1))
    {
        int add=atoi(add_number);
        final_quantity = find_goods->quantity + add;
        if(final_quantity > 99999)
        {
            lv_obj_t * mbox = lv_msgbox_create(NULL, "Reminder", "The increase is too large. \nThe maximum quantity is 99,999.", NULL, true);
            lv_obj_center(mbox);
            return;
        }
        find_goods->quantity = final_quantity;
        snprintf(log_buf,sizeof(log_buf),"%s added %d more.",find_goods->name,add);
        log_add(LOG_UPDATE_ITEM,cur_user.username,cur_user.identity,log_buf);
    }
    if(reduce_len != 0 && (select_flag == 0))
    {
        int reduce = atoi(reduce_number);
        final_quantity = find_goods->quantity - reduce;
        if(final_quantity<0)
        {
            lv_obj_t * mbox = lv_msgbox_create(NULL, "Reminder", "The quantity of the goods cannot be negative.\n The minimum quantity is 0.", NULL, true);
            lv_obj_center(mbox);
            return;
        }
        find_goods->quantity = final_quantity;
        snprintf(log_buf,sizeof(log_buf),"The number of %s has decreased by %d.",find_goods->name,reduce);
        log_add(LOG_UPDATE_ITEM,cur_user.username,cur_user.identity,log_buf);
    }
    file_save_data(goods_list,"goodsdata.txt");
    
    Inventory_show_search_goods_cb(id,data->goods_table_cont);
}

/*----库存管理---*/
void inventory_goods_cb(void)
{
    // 创建内容容器
    lv_obj_t * cont = lv_win_get_content(input_win);
    lv_obj_set_style_border_width(cont, 0, 0);
    lv_obj_set_style_pad_all(cont, 0, 0);

    //2.创建搜索框
    lv_obj_t * find_goods_cont = lv_obj_create(cont);
    lv_obj_set_size(find_goods_cont, 800,80);
    lv_obj_set_style_border_width(find_goods_cont, 0, 0);
    lv_obj_set_style_pad_all(find_goods_cont, 0, 0);
    lv_obj_remove_style(find_goods_cont, NULL, LV_PART_SCROLLBAR);

    //创建一个表格容器
    lv_obj_t * goods_table_cont = lv_obj_create(cont);
    lv_obj_set_size(goods_table_cont, 800,350);
    lv_obj_align_to(goods_table_cont,find_goods_cont,LV_ALIGN_OUT_BOTTOM_MID,0,0);
    lv_obj_set_style_border_width(goods_table_cont, 0, 0);
    lv_obj_set_style_pad_all(goods_table_cont, 0, 0);
    lv_obj_remove_style(goods_table_cont, NULL, LV_PART_SCROLLBAR);

    //  2.1   id
    lv_obj_t * find_id_label = lv_label_create(find_goods_cont);
    lv_label_set_text(find_id_label, "ID:");
    lv_obj_align(find_id_label, LV_ALIGN_LEFT_MID, 50, 0);

    lv_obj_t *find_goods_id = lv_textarea_create(find_goods_cont);
    lv_obj_align(find_goods_id,LV_ALIGN_LEFT_MID,80,0);
    lv_obj_set_size(find_goods_id,150,50);
    lv_obj_remove_style(find_goods_id, NULL, LV_PART_SCROLLBAR);
    lv_textarea_set_placeholder_text(find_goods_id, "goods id");
    lv_textarea_set_accepted_chars(find_goods_id, "0123456789");
    lv_textarea_set_max_length(find_goods_id, 8);
    lv_textarea_set_one_line(find_goods_id, true);
    lv_obj_add_event_cb(find_goods_id,keyboard_event_cb,LV_EVENT_ALL,NULL);

    FindData *find_data =malloc(sizeof(FindData));
    if (find_data == NULL) {
        printf("Memory allocation failed\n");
        return;
    }
    find_data->find_goods_id_ta = find_goods_id;
    find_data->find_goods_name_ta= NULL;
    find_data->find_goods_type_dp= NULL;
    find_data->goods_table_cont = goods_table_cont;
    //  2.2  查找按钮
    lv_obj_t *find_btn = lv_btn_create(find_goods_cont);
    lv_obj_set_size(find_btn,80,45);
    lv_obj_align_to(find_btn,find_goods_id, LV_ALIGN_OUT_RIGHT_MID, 10, 0);
    lv_obj_add_event_cb(find_btn,find_goods_event_cb,LV_EVENT_CLICKED,(void *)find_data);
    lv_obj_t * find_btn_label = lv_label_create(find_btn);
    lv_label_set_text(find_btn_label, "Search");
    lv_obj_center(find_btn_label);


    //  2.3   增加
    lv_obj_t *goods_add_ta = lv_textarea_create(find_goods_cont);
    lv_obj_align(goods_add_ta,LV_ALIGN_LEFT_MID,340,0);
    lv_obj_set_size(goods_add_ta,150,50);
    lv_obj_remove_style(goods_add_ta, NULL, LV_PART_SCROLLBAR);
    lv_textarea_set_placeholder_text(goods_add_ta, "add number");
    lv_textarea_set_accepted_chars(goods_add_ta, "0123456789");
    lv_textarea_set_max_length(goods_add_ta, 6);
    lv_textarea_set_one_line(goods_add_ta, true);
    lv_obj_add_event_cb(goods_add_ta,keyboard_event_cb,LV_EVENT_ALL,NULL);

    lv_obj_t *add_quantity_btn = lv_btn_create(find_goods_cont);
    lv_obj_align(add_quantity_btn,LV_ALIGN_LEFT_MID,510,0);
    lv_obj_set_size(add_quantity_btn,40,40);
    lv_obj_set_style_radius(add_quantity_btn,20,0);
    lv_obj_t *add_label = lv_label_create(add_quantity_btn);
    lv_label_set_text(add_label,LV_SYMBOL_PLUS);
    lv_obj_center(add_label);

    //  2.3   减少
    lv_obj_t *goods_Reduce_ta = lv_textarea_create(find_goods_cont);
    lv_obj_align(goods_Reduce_ta,LV_ALIGN_LEFT_MID,560,0);
    lv_obj_set_size(goods_Reduce_ta,150,50);
    lv_obj_remove_style(goods_Reduce_ta, NULL, LV_PART_SCROLLBAR);
    lv_textarea_set_placeholder_text(goods_Reduce_ta, "Reduce number");
    lv_textarea_set_accepted_chars(goods_Reduce_ta, "0123456789");
    lv_textarea_set_max_length(goods_Reduce_ta, 6);
    lv_textarea_set_one_line(goods_Reduce_ta, true);
    lv_obj_add_event_cb(goods_Reduce_ta,keyboard_event_cb,LV_EVENT_ALL,NULL);

    lv_obj_t *Reduce_quantity_btn = lv_btn_create(find_goods_cont);
    lv_obj_align(Reduce_quantity_btn,LV_ALIGN_LEFT_MID,720,0);
    lv_obj_set_size(Reduce_quantity_btn,40,40);
    lv_obj_set_style_radius(Reduce_quantity_btn,20,0);

    lv_obj_t *Reduce_label = lv_label_create(Reduce_quantity_btn);
    lv_label_set_text(Reduce_label,LV_SYMBOL_MINUS);
    lv_obj_center(Reduce_label);

    //用来存储id、加、减数量
    FindData *data =malloc(sizeof(FindData));
    if (data == NULL) {
        printf("Memory allocation failed\n");
        return;
    }
    data->find_goods_id_ta = find_goods_id;
    data->find_goods_name_ta= goods_add_ta;
    data->find_goods_type_dp= goods_Reduce_ta;
    data->goods_table_cont = goods_table_cont;

    lv_obj_add_event_cb(add_quantity_btn,change_quantity_event_cb,LV_EVENT_CLICKED,data);
    lv_obj_add_event_cb(Reduce_quantity_btn,change_quantity_event_cb,LV_EVENT_CLICKED,data);
    show_all_goods_cb(goods_table_cont);

}


//价格编辑回调
void price_modif_event_cb(lv_event_t *e)
{
    FindData *data = (FindData*)lv_event_get_user_data(e);
    if(data == NULL)
    {
        printf("Error:selete_data is NULL\n");
        return;
    }
    char *data_id = lv_textarea_get_text(data->find_goods_id_ta);
    char *price_edit = lv_textarea_get_text(data->find_goods_name_ta);
    price_edit[strlen(price_edit)] = '\0';

    int id_len = strlen(data_id);
    int price_len = strlen(price_edit);

    if(id_len == 0)
    {
        lv_obj_t * mbox = lv_msgbox_create(NULL, "Reminder", "Please enter the product ID.", NULL, true);
        lv_obj_center(mbox);
        return;
    }

    if(price_len == 0)
    {
        lv_obj_t * mbox = lv_msgbox_create(NULL, "Reminder", "Please enter the price that needs to be modified.", NULL, true);
        lv_obj_center(mbox);
        return;
    }

    int id =atoi(data_id);
    float price = atof(price_edit);

    Goodslist *find_goods = find_node(goods_list,id);
    if(price < 0)
    {
        lv_obj_t * mbox = lv_msgbox_create(NULL, "Reminder", "The price cannot be negative.\nThe minimum price is 0.", NULL, true);
        lv_obj_center(mbox);
        return;
    }
    find_goods->price = price;
    file_save_data(goods_list,"goodsdata.txt");
    char log_buf[256];
    snprintf(log_buf,sizeof(log_buf),"Update the price of %s to %.2f yuan.",find_goods->name,find_goods->price);
    log_add(LOG_UPDATE_ITEM,cur_user.username,cur_user.identity,log_buf);
    Inventory_show_search_goods_cb(id,data->goods_table_cont);

}

/*价格编辑界面*/
void goods_price_editor_cb(void)
{
    // 创建内容容器
    lv_obj_t * cont = lv_win_get_content(input_win);
    lv_obj_set_style_border_width(cont, 0, 0);
    lv_obj_set_style_pad_all(cont, 0, 0);

    //2.创建搜索框
    lv_obj_t * find_goods_cont = lv_obj_create(cont);
    lv_obj_set_size(find_goods_cont, 800,80);
    lv_obj_set_style_border_width(find_goods_cont, 0, 0);
    lv_obj_set_style_pad_all(find_goods_cont, 0, 0);
    lv_obj_remove_style(find_goods_cont, NULL, LV_PART_SCROLLBAR);

    //创建一个表格容器
    lv_obj_t * goods_table_cont = lv_obj_create(cont);
    lv_obj_set_size(goods_table_cont, 800,350);
    lv_obj_align_to(goods_table_cont,find_goods_cont,LV_ALIGN_OUT_BOTTOM_MID,0,0);
    lv_obj_set_style_border_width(goods_table_cont, 0, 0);
    lv_obj_set_style_pad_all(goods_table_cont, 0, 0);
    lv_obj_remove_style(goods_table_cont, NULL, LV_PART_SCROLLBAR);

    //  2.1   id
    lv_obj_t * find_id_label = lv_label_create(find_goods_cont);
    lv_label_set_text(find_id_label, "ID:");
    lv_obj_align(find_id_label, LV_ALIGN_LEFT_MID, 50, 0);

    lv_obj_t *find_goods_id = lv_textarea_create(find_goods_cont);
    lv_obj_align(find_goods_id,LV_ALIGN_LEFT_MID,80,0);
    lv_obj_set_size(find_goods_id,150,50);
    lv_obj_remove_style(find_goods_id, NULL, LV_PART_SCROLLBAR);
    lv_textarea_set_placeholder_text(find_goods_id, "goods id");
    lv_textarea_set_accepted_chars(find_goods_id, "0123456789");
    lv_textarea_set_max_length(find_goods_id, 8);
    lv_textarea_set_one_line(find_goods_id, true);
    lv_obj_add_event_cb(find_goods_id,keyboard_event_cb,LV_EVENT_ALL,NULL);

    FindData *find_data =malloc(sizeof(FindData));
    if (find_data == NULL) {
        printf("Memory allocation failed\n");
        return;
    }
    find_data->find_goods_id_ta = find_goods_id;
    find_data->find_goods_name_ta= NULL;
    find_data->find_goods_type_dp= NULL;
    find_data->goods_table_cont = goods_table_cont;
    //  2.2  查找按钮
    lv_obj_t *find_btn = lv_btn_create(find_goods_cont);
    lv_obj_set_size(find_btn,80,45);
    lv_obj_align_to(find_btn,find_goods_id, LV_ALIGN_OUT_RIGHT_MID, 10, 0);
    lv_obj_add_event_cb(find_btn,find_goods_event_cb,LV_EVENT_CLICKED,(void *)find_data);
    lv_obj_t * find_btn_label = lv_label_create(find_btn);
    lv_label_set_text(find_btn_label, "Search");
    lv_obj_center(find_btn_label);


    //  2.3   价格修改
    lv_obj_t *goods_price_modif_ta = lv_textarea_create(find_goods_cont);
    lv_obj_align(goods_price_modif_ta,LV_ALIGN_LEFT_MID,340,0);
    lv_obj_set_size(goods_price_modif_ta,150,50);
    lv_obj_remove_style(goods_price_modif_ta, NULL, LV_PART_SCROLLBAR);
    lv_textarea_set_placeholder_text(goods_price_modif_ta, "edit price");
    lv_textarea_set_accepted_chars(goods_price_modif_ta, "0123456789.");
    lv_textarea_set_max_length(goods_price_modif_ta, 6);
    lv_textarea_set_one_line(goods_price_modif_ta, true);
    lv_obj_add_event_cb(goods_price_modif_ta,keyboard_event_cb,LV_EVENT_ALL,NULL);

    lv_obj_t *goods_price_modif_btn = lv_btn_create(find_goods_cont);
    lv_obj_align(goods_price_modif_btn,LV_ALIGN_LEFT_MID,510,0);
    lv_obj_set_size(goods_price_modif_btn,40,40);
    lv_obj_set_style_radius(goods_price_modif_btn,20,0);
    lv_obj_t *price_modif_label = lv_label_create(goods_price_modif_btn);
    lv_label_set_text(price_modif_label,LV_SYMBOL_EDIT);
    lv_obj_center(price_modif_label);

    //用来存储id、要修改的价格
    FindData *data =malloc(sizeof(FindData));
    if (data == NULL) {
        printf("Memory allocation failed\n");
        return;
    }
    data->find_goods_id_ta = find_goods_id;
    data->find_goods_name_ta= goods_price_modif_ta;
    data->find_goods_type_dp= NULL;
    data->goods_table_cont = goods_table_cont;
    lv_obj_add_event_cb(goods_price_modif_btn,price_modif_event_cb,LV_EVENT_CLICKED,data);
    show_all_goods_cb(goods_table_cont);
}